package com.ruoyi.test.ht;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ruoyi.test.bd.Main.writeListToFile;

public class Ht {

    static Map<String,String> c = new HashMap<>();

    public static void main(String[] args) throws IOException {

        List<String> lines = Files.readAllLines(Paths.get("D:\\Sh4\\ruoyi-plus-tdengine\\ruoyi-admin\\src\\test\\java\\com\\ruoyi\\test\\ht\\config.txt"));
        for (String line : lines) {
            String[] split = line.split("=");
            c.put(split[0] ,split[1]);

        }


        String projectPath = "D:\\liangma_new\\lup_datacenter_business"; // 替换为你的Java项目路径
//        String projectPath = "D:\\liangma_new\\lup-event-workbill"; // 替换为你的Java项目路径
        searchInDirectory(new File(projectPath));
    }

    private static void searchInDirectory(File directory) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        searchInDirectory(file);
                    } else if (file.getName().endsWith(".java")) {
                        searchInFile(file);
                    }
                }
            }
        }
    }

    private static void searchInFile(File file) {
        try {
            List<String> lines = Files.readAllLines(file.toPath());
            List<String> linesNew = new ArrayList<>();
            Pattern pattern = Pattern.compile("(throw\\s+[^\\s;]+\\s*|\\bResult\\.fail\\b|\\bResult\\.success\\b|\\bAssert\\.notNull\\b|\\bAssert\\.hasText\\b|\\bAssert\\.isTrue\\b|\\bAssert\\.(?!notNull|hasText|isTrue)\\w+\\b|\\benum\\s+\\w+\\b)");
            int lineNumber = 0;
            AtomicInteger i = new AtomicInteger();
            for (String line : lines) {
                lineNumber++;
                AtomicBoolean add = new AtomicBoolean(false);
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    if (line.trim() .contains("e.getMessage()")){

                    }else {
                        //D:\liangma_new\lup_datacenter_business\lup-cms
                        if (file.getPath().contains("lup-workflow")
//                            || file.getPath().contains("lup-datacenter-server")
//                            || file.getPath().contains("lup-datacenter-common")
//                            || file.getPath().contains("lup-datacenter-websocket")
//                            || file.getPath().contains("lup-datacenter-api")
//                            || file.getPath().contains("lup-datacenter-mqtt")
//                            || file.getPath().contains("lup-mqtt")
                        ) {
                            String trim = line.trim();
                            String[] parts = file.getPath().split("\\\\");
                            String part = parts[parts.length - 1];
                            String className = part.split("\\.")[0].toLowerCase();

                            c.forEach((k,v)->{
                                if (k.contains(className)){
                                    if (trim.contains(v)){

                                        try{
                                            String s = line.replaceAll("\""+v+"\"", "getMessage(\"" + k + "\")");
                                            linesNew.add(s);
                                            add.set(true);
                                            System.out.printf("111111111");
                                            i.set(1);
                                        }catch (Exception e){
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            });

                        }

                    }

                }
                if (!add.get()){
                    linesNew.add(line);
                }
            }
            if (i.get() == 1){
                //重新文件
                writeListToFile(linesNew, file.getPath());
                System.out.printf("重新文件"+ file.getPath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
